
 const { query } = require('express');
const express =  require('express')
 const router = express.Router();

 //导入模型
 const dbQuery = require('../model/model.js');
const { route } = require('./router.js');



//获取分类
router.get('/cate', async(req,res)=>{
    let sql = 'select * from articletable where Show_timeornot = 1';
    let result = await dbQuery(sql)
    res.json(result)

})



// 获取文章详情
router.get('/cate',async (req,res)=>{
    let {id} = req.query;
    id = parseInt(id)
    id = isNaN(id) ?  0  : id;

    let sql = '`select t1.*,t2.name from article t1 left join articletable t2  on  t1.sort = t2.id where t1.id  = ${id}';
    let result = await query(sql)
    res.json(result[0] || {})
})

// 获取首页文章
router.get('/article', async (req,res)=>{
    let {page =1,pagesize=5} = req.query;

    let offset = (page - 1) * pagesize

    let sql = `select t1.*,t2.name from article t1 left join articletable t2  on  t1.sort = t2.id where is_delete =0  limit ${offset},${pagesize}`
    result=  await dbQuery(sql)

    res.json(result)
})
router.get('*',(req,res)=>{
    res.json({message:404})
})

module.exports = router;